package com.wyp168.leetcode;

public class Recursion03 {
    public static void main(String[] args) {
        int[][] map = new int[8][7];
        for (int i = 0; i <= 6; i++) {
            map[0][i] = 1;
            map[7][i] = 1;
        }
        for (int i = 0; i <= 7; i++) {
            map[i][0] = 1;
            map[i][6] = 1;
        }
        map[3][1] = 1;
        map[3][2] = 1;
        for(int i = 0 ; i <= 7; i++) {
            for (int j = 0; j <= 6; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }
        migong(map,1,1);

        System.out.println("最终结果");
        for(int i = 0 ; i <= 7; i++) {
            for (int j = 0; j <= 6; j++) {
                System.out.print(map[i][j] + " ");
            }
            System.out.println();
        }

    }

    public static boolean migong(int[][] map , int i, int j){
        //先设置递归的出口，当走到第7行第6列的时候 成功，并且游戏退出
        if (i == 6 && j ==5) {
            map[i][j] = 2;
            return true;
        } else {
            if(map[i][j] == 0) {
                //按照我们指定的路线行走：下 右 上 左
                map[i][j] = 2;
                if (migong(map,i + 1,j)) {
                    return true;
                } else if (migong(map,i,j + 1)) {
                    return true;
                } else if(migong(map,i-1,j)) {
                    return true;
                } else if(migong(map,i,j - 1)) {
                    return true;
                } else {
                    map[i][j] = 3;
                    return false;
                }
            } else {
                return false;
            }

        }
    }
}
